package com.cnzha.mr.table;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Objects;

/**
 * @Author: zhaoxin
 * @email: zdkk@foxmail.com
 * @Date: 2020/4/5 22:40
 */
public class TableReduce extends Reducer<Text, TableBean, TableBean, NullWritable> {

    @Override
    protected void reduce(Text key, Iterable<TableBean> values, Context context) throws IOException, InterruptedException {
        ArrayList<TableBean> tableBeans = new ArrayList<>();
        TableBean tb = null;
        for (TableBean tableBean : values) {
            TableBean temp = new TableBean();
            String tableName = tableBean.getTableName();
            if (Objects.equals(tableName, "order")) {
                try {
                    BeanUtils.copyProperties(temp,tableBean);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
                tableBeans.add(temp);
            } else {
                try {
                    BeanUtils.copyProperties(temp,tableBean);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
                tb = temp;
            }
        }
        for (TableBean tableBean : tableBeans) {
            tableBean.setpName(tb.getpName());
            context.write(tableBean, NullWritable.get());
        }


    }


}
